package tool

import (
	_ "embed"
	"errors"
	"translate/config"
	"translate/tool/translate"
)

// Translate 翻译文本
func Translate(language, text string) (*translate.TranslateResult, error) {

	switch config.Conf.Model {

	case "有道翻译":
		daoTranslate, err := translate.YouDaoTranslate(language, text)
		//翻译失败
		if err != nil {
			return nil, err
		}

		if daoTranslate.ErrorCode != "0" {
			return nil, errors.New("有道错误代码：" + daoTranslate.ErrorCode)
		}

		//翻译成功
		return &translate.TranslateResult{
			Code:    daoTranslate.ErrorCode,
			Content: daoTranslate.Query,
			Result:  daoTranslate.Translation[0],
		}, err
	case "OpenAI":
		openAIConfig := config.Conf.OpenAI
		result, err := translate.OpenAITranslate(
			openAIConfig.Url,
			openAIConfig.ApiKey,
			openAIConfig.ModelName,
			text, language,
			openAIConfig.Prompt)
		return result, err
	}
	return nil, errors.New("未配置翻译模型")
}
